package com.common.UITags;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.common.GlobalPara;
/**
 * 
 * 类描述：body标签
 * 由于tab页面不能有body,head,html等标签，但是一个页面没有body会让人看着很不爽
 * 所以为了让看起来比较爽，而且也为了能够后期的扩展如客户化扩展，决定写这么一个标签
 * @author 张龙
 * @version 1.0
 * 创建时间 2015-4-2
 */
public class BodyTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	JspWriter out;
	StringBuffer sBF = new StringBuffer();
	private String uuid;
	
	public int doStartTag() throws JspException {
		try{
			JspWriter out = pageContext.getOut();
			this.uuid = getUUID();
			String currentPath = (String)((HttpServletRequest)pageContext.getRequest()).getAttribute(GlobalPara.JSP_PATH_NAME);
			sBF.setLength(0);
			sBF.append("<input id='systemM_path' type='hidden' value='" + currentPath+"'/>");
			sBF.append("<div id=\"" + uuid);
			sBF.append("\"");
			sBF.append("style=\"width: 100%;height: 100%;\"");
			sBF.append(" >");
			out.print(sBF.toString());
		}catch (IOException e) {
			throw new JspException("标签往前台写数据时异常");
		}catch(Exception e){
			throw new JspException("其他异常");
		}
	
		return EVAL_BODY_INCLUDE;
	}
	
	public int doEndTag() throws JspException {
		sBF.setLength(0);
		sBF.append("<script type=\"text/javascript\">");
		sBF.append("$.parser.parse(\"#"+this.uuid+"\")");
		sBF.append("</script>");
		sBF.append("</div>");
		try {
			pageContext.getOut().print(sBF.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	private String getUUID() {
		return java.util.UUID.randomUUID().toString().replace("-", "");
	}
}
